/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */
// 09/25/2011 Paul.  sIMAGE_SERVER is usually blank, but is used with the HTML5 client. 
var sIMAGE_SERVER    = '';
var sPLATFORM_LAYOUT = '';

function SplendidUI_CacheNext(arrMODULE_NAMES, callback)
{
	try
	{
		if ( arrMODULE_NAMES.length > 0 )
		{
			var sMODULE_NAME = arrMODULE_NAMES.pop();
			callback(2, 'Loading ' + sMODULE_NAME + ' cache');
			
			var bgPage = chrome.extension.getBackgroundPage();
			bgPage.Terminology_LoadModule(sMODULE_NAME, function(status, message)
			{
				callback(2, 'Loaded ' + sMODULE_NAME + ' Terminology');
				var sGRID_NAME = sMODULE_NAME + '.ListView' + sPLATFORM_LAYOUT;
				bgPage.ListView_LoadLayout(sGRID_NAME, function(status, message)
				{
					callback(2, 'Loaded ' + sGRID_NAME);
					var sDETAIL_NAME = sMODULE_NAME + '.DetailView' + sPLATFORM_LAYOUT;
					bgPage.DetailView_LoadLayout(sDETAIL_NAME, function(status, message)
					{
						callback(2, 'Loaded ' + sDETAIL_NAME);
						bgPage.DynamicButtons_LoadLayout(sDETAIL_NAME, function(status, message)
						{
							callback(2, 'Loaded ' + sDETAIL_NAME + ' buttons');
							var sEDIT_NAME = sMODULE_NAME + '.EditView' + sPLATFORM_LAYOUT;
							bgPage.EditView_LoadLayout(sEDIT_NAME, function(status, message)
							{
								callback(2, 'Loaded ' + sEDIT_NAME);
								bgPage.DynamicButtons_LoadLayout(sEDIT_NAME, function(status, message)
								{
									callback(2, 'Loaded ' + sEDIT_NAME + ' buttons');
									sEDIT_NAME = sMODULE_NAME + '.SearchBasic' + sPLATFORM_LAYOUT;
									bgPage.EditView_LoadLayout(sEDIT_NAME, function(status, message)
									{
										callback(2, 'Loaded ' + sEDIT_NAME + ' search');
										sGRID_NAME = sMODULE_NAME + '.PopupView' + sPLATFORM_LAYOUT;
										bgPage.ListView_LoadLayout(sGRID_NAME, function(status, message)
										{
											callback(2, 'Loaded ' + sGRID_NAME + ' popup');
											sEDIT_NAME = sMODULE_NAME + '.SearchPopup' + sPLATFORM_LAYOUT;
											bgPage.EditView_LoadLayout(sEDIT_NAME, function(status, message)
											{
												callback(2, 'Loaded ' + sEDIT_NAME);
												SplendidUI_CacheNext(arrMODULE_NAMES, callback);
											});
										});
									});
								});
							});
						});
					});
				});
			});
		}
		else
		{
			callback(2, '');
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'SplendidUI_Cache');
	}
}

function SplendidUI_Cache(callback)
{
	try
	{
		var nCacheItemCount = 0;
		var bgPage = chrome.extension.getBackgroundPage();
		bgPage.DetailViewRelationships_LoadLayout('TabMenu' + sPLATFORM_LAYOUT, function(status, message)
		{
			if ( status == 1 )
			{
				var arrMODULE_NAMES = new Array();
				// 10/03/2011 Paul.  DetailViewRelationships_LoadLayout returns the layout. 
				var arrDetailViewRelationship = message;
				//var arrDetailViewRelationship = bgPage.SplendidCache.DetailViewRelationships('TabMenu' + sPLATFORM_LAYOUT);
				for ( var i = 0; i < arrDetailViewRelationship.length; i++ )
				{
					var sMODULE_NAME = arrDetailViewRelationship[i].MODULE_NAME;
					arrMODULE_NAMES.push(sMODULE_NAME);
				}
				// 10/06/2011 Paul.  We can't use a simple loop as JavaScript will re-use existing variables within the scope. 
				// SplendidUI_CacheNext makes sure that each fetch is called sequentially. 
				SplendidUI_CacheNext(arrMODULE_NAMES, callback);
			}
			else
			{
				callback(-1, message);
			}
		});
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'SplendidUI_Cache');
	}
}

function SplendidUI_Clear(sLayoutPanel, sActionsPanel)
{
	try
	{
		var divHeaderPanel = document.getElementById(sLayoutPanel + '_Header');
		if ( divHeaderPanel != null && divHeaderPanel.childNodes != null )
		{
			while ( divHeaderPanel.childNodes.length > 0 )
			{
				divHeaderPanel.removeChild(divHeaderPanel.firstChild);
			}
		}
		var divLayoutPanel = document.getElementById(sLayoutPanel);
		if ( divLayoutPanel.childNodes != null )
		{
			while ( divLayoutPanel.childNodes.length > 0 )
			{
				divLayoutPanel.removeChild(divLayoutPanel.firstChild);
			}
		}
		var divActionsPanel = document.getElementById(sActionsPanel);
		if ( divActionsPanel.childNodes != null )
		{
			while ( divActionsPanel.childNodes.length > 0 )
			{
				divActionsPanel.removeChild(divActionsPanel.firstChild);
			}
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'SplendidUI_Clear');
	}
}

function SplendidUI_ModuleHeader(sLayoutPanel, sActionsPanel, sMODULE_NAME, sSUB_TITLE)
{
	try
	{
		var sMODULE_TITLE  = '.moduleList.' + sMODULE_NAME;
		var divHeaderPanel = document.getElementById(sLayoutPanel + '_Header');
		if ( divHeaderPanel != null && divHeaderPanel.childNodes != null )
		{
			while ( divHeaderPanel.childNodes.length > 0 )
			{
				divHeaderPanel.removeChild(divHeaderPanel.firstChild);
			}
		}
		//<asp:Table CssClass="moduleTitle" runat="server">
		//	<asp:TableRow>
		//		<asp:TableCell Wrap="false">
		//			<h2><span class="pointer">&raquo;</span></h2>
		//		</asp:TableCell>
		//	</asp:TableRow>
		//</asp:Table>
		if ( divHeaderPanel != null )
		{
			var tblHeader = document.createElement('table');
			tblHeader.cellSpacing = 1;
			tblHeader.cellPadding = 0;
			tblHeader.border      = 0;
			tblHeader.width       = '100%';
			tblHeader.className   = 'moduleTitle';
			divHeaderPanel.appendChild(tblHeader);
			
			var tbody = document.createElement('tbody');
			tblHeader.appendChild(tbody);
			var tr = document.createElement('tr');
			tbody.appendChild(tr);
			var td = document.createElement('td');
			tr.appendChild(td);
			var h2 = document.createElement('h2');
			td.appendChild(h2);
			
			var lnkModule = document.createElement('a');
			h2.appendChild(lnkModule);
			lnkModule.innerHTML = L10n.Term(sMODULE_TITLE);
			lnkModule.href = 'javascript:void(0);';
			lnkModule.onclick = function()
			{
				var sGRID_NAME = sMODULE_NAME + '.ListView' + sPLATFORM_LAYOUT;
				ListViewUI_Reset(sLayoutPanel, sMODULE_NAME);
				ListViewUI_Load(sLayoutPanel, sActionsPanel, sMODULE_NAME, sGRID_NAME, null, function(status, message)
				{
					if ( status == 0 || status == 1 )
					{
						sEDITVIEW_MODULE   = null;
						sEDITVIEW_ID       = null;
						sDETAILVIEW_MODULE = null;
						sDETAILVIEW_ID     = null;
					}
				});
			};
			var raquo = document.createElement('span');
			h2.appendChild(raquo);
			raquo.className = 'pointer';
			raquo.innerHTML = '&raquo;';
			var txtTitle = document.createTextNode(L10n.Term(sSUB_TITLE));
			h2.appendChild(txtTitle);
		}
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'SplendidUI_ModuleHeader'));
	}
}

function SplendidUI_ListHeader(sLayoutPanel, sTITLE)
{
	try
	{
		//<asp:Table SkinID="tabFrame" CssClass="h3Row" runat="server">
		//	<asp:TableRow>
		//		<asp:TableCell Wrap="false">
		//			<h3><asp:Image SkinID="h3Arrow" Runat="server" />&nbsp;<asp:Label Text='<%# L10n.Term(sTitle) %>' runat="server" /></h3>
		//		</asp:TableCell>
		//	</asp:TableRow>
		//</asp:Table>
		var divMainLayoutPanel = document.getElementById(sLayoutPanel);
		if ( divMainLayoutPanel != null )
		{
			var tblHeader = document.createElement('table');
			tblHeader.cellSpacing = 1;
			tblHeader.cellPadding = 0;
			tblHeader.border      = 0;
			tblHeader.width       = '100%';
			tblHeader.className   = 'tabFrame';
			divMainLayoutPanel.appendChild(tblHeader);
			
			var tbody = document.createElement('tbody');
			tblHeader.appendChild(tbody);
			var tr = document.createElement('tr');
			tbody.appendChild(tr);
			var td = document.createElement('td');
			tr.appendChild(td);
			var h3 = document.createElement('h3');
			td.appendChild(h3);
			
			var img = document.createElement('img');
			h3.appendChild(img);
			img.align       = 'absmiddle';
			img.height      = 11;
			img.width       = 11;
			img.borderWidth = 0;
			img.src         = sIMAGE_SERVER + 'App_Themes/Six/images/h3Arrow.gif';
			
			var nbsp = String.fromCharCode(160);
			var txtTitle = document.createTextNode(nbsp + L10n.Term(sTITLE));
			h3.appendChild(txtTitle);
		}
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'SplendidUI_ListHeader'));
	}
}

function SplendidUI_NextCustomList(arrLISTS, callback)
{
	try
	{
		if ( arrLISTS.length > 0 )
		{
			var sLIST_NAME = arrLISTS.pop();
			
			var bgPage = chrome.extension.getBackgroundPage();
			bgPage.Terminology_LoadCustomList(sLIST_NAME, function(status, message)
			{
				callback(2, 'Loaded ' + sLIST_NAME);
				SplendidUI_NextCustomList(arrLISTS, callback);
			});
		}
		else
		{
			callback(1, '');
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'SplendidUI_NextCustomList');
	}
}


function SplendidUI_LoadCustomLists(callback)
{
	try
	{
		var arrLISTS = new Array();
		arrLISTS.push('Currencies'       );
		arrLISTS.push('Release'          );
		arrLISTS.push('ContractTypes'    );
		arrLISTS.push('AssignedUser'     );
		//arrLISTS.push('Manufacturers'    );
		//arrLISTS.push('Discounts'        );
		//arrLISTS.push('Shippers'         );
		//arrLISTS.push('Regions'          );
		//arrLISTS.push('ProductTypes'     );
		//arrLISTS.push('ProductCategories');
		SplendidUI_NextCustomList(arrLISTS, callback);
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'SplendidUI_LoadCustomLists');
	}
}

function SplendidUI_Init(sLayoutPanel, sActionsPanel, sMODULE_NAME, rowDefaultSearch, callback)
{
	try
	{
		callback(2, 'Authenticating with CRM.');
		var bgPage = chrome.extension.getBackgroundPage();
		bgPage.AuthenticatedMethod(function(status, message)
		{
			if ( status == 1 )
			{
				callback(2, 'Loading config.');
				bgPage.Application_Config(function(status, message)
				{
					if ( status == 0 || status == 1 )
					{
						callback(2, 'Loading modules.');
						bgPage.Application_Modules(function(status, message)
						{
							if ( status == 0 || status == 1 )
							{
								callback(2, 'Loading global terminology.');
								bgPage.Terminology_LoadGlobal(function(status, message)
								{
									if ( status == 0 || status == 1 )
									{
										SplendidUI_LoadCustomLists(function(status, message)
										{
											if ( status == 0 || status == 1 )
											{
												// 10/02/2011 Paul.  Status of 3 means that the globals are done loading. 
												callback(3, '');
												callback(2, 'Loading tab menu for ' + sMODULE_NAME);
												// 10/15/2011 Paul.  sTabMenuCtl is a div tag now so that we can do more with the panel. 
												TabMenuUI_Load('ctlTabMenu', sLayoutPanel, sActionsPanel, sMODULE_NAME, function(status, message)
												{
													// 01/01/2012 Paul.  Move loading teams before loading the list view as the list might display team names. 
													callback(2, 'Loading teams.');
													bgPage.Application_Teams(function(status, message)
													{
														if ( status == 0 || status == 1 )
														{
															var sGRID_NAME = sMODULE_NAME + '.ListView' + sPLATFORM_LAYOUT;
															callback(2, 'Loading ' + sGRID_NAME + '.');
															ListViewUI_Reset(sLayoutPanel, sMODULE_NAME);
															ListViewUI_Load(sLayoutPanel, sActionsPanel, sMODULE_NAME, sGRID_NAME, rowDefaultSearch, function(status, message)
															{
																if ( status == 0 || status == 1 )
																{
																	callback(1, null);
																}
																else
																{
																	callback(status, message);
																}
															});
														}
														else
														{
															callback(status, message);
														}
													});
												});
											}
											else
											{
												callback(status, message);
											}
										});
									}
									else
									{
										callback(status, message);
									}
								});
							}
							else
							{
								callback(status, message);
							}
						});
					}
					else
					{
						callback(status, message);
					}
				});
			}
			else
			{
				callback(status, message);
			}
		});
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'SplendidUI_Init'));
	}
}

function SplendidUI_ReInit(sLayoutPanel, sActionsPanel, sMODULE_NAME)
{
	try
	{
		if ( sMODULE_NAME == null )
			sMODULE_NAME = sSTARTUP_MODULE;
		SplendidUI_Init(sLayoutPanel, sActionsPanel, sMODULE_NAME, null, function(status, message)
		{
			if ( status == 3 )
			{
				LoginViewUI_UpdateHeader(sLayoutPanel, sActionsPanel, true)
			}
			else if ( status == 1 )
			{
				SplendidError.SystemMessage('');
			}
			else
			{
				SplendidError.SystemMessage(message);
			}
		});
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'SplendidUI_ReInit'));
	}
}

